package com.app.service.impl;


import com.app.mapper.JobFairSceneMapper;
import com.app.model.JobFairCompanyModel;
import com.app.model.JobFairSceneModel;
import com.app.model.VideoTrainModel;
import com.app.service.JobFairCompanyService;
import com.app.service.JobFairSceneService;
import com.app.util.CommonHelp;
import com.app.util.StringTool;
import com.app.vo.JobFairSceneVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class JobFairSceneServiceImpl implements JobFairSceneService {

    @Resource
    private JobFairSceneMapper jobFairSceneMapper;

    @Autowired
    JobFairCompanyService jobFairCompanyService;

    @Override
    public JobFairSceneModel findAllById(int id) {
        QueryWrapper<JobFairSceneModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        List<JobFairSceneModel> userList = jobFairSceneMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public void update(JobFairSceneModel model) {
        jobFairSceneMapper.updateById(model);
    }

    @Override
    public List<JobFairSceneModel> findAllByCrtTime(JobFairSceneModel model) {
        QueryWrapper<JobFairSceneModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scene_type", model.getSceneType());
        queryWrapper.orderByDesc("event_time_start");
        List<JobFairSceneModel> userList = jobFairSceneMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public Page<JobFairSceneModel> getJobFairList(JobFairSceneModel model) {
        QueryWrapper<JobFairSceneModel> queryWrapper = new QueryWrapper<>();
        if(StringTool.isNotEmpty(model.getStartTime()))
        {
            queryWrapper.ge("event_time_start",model.getStartTime());
        }
        if(StringTool.isNotEmpty(model.getEndTime()))
        {
            queryWrapper.le("event_time_start",model.getEndTime());
        }
        if(StringTool.isNotEmpty(model.getSceneType()))
        {
            queryWrapper.like("scene_type",model.getSceneType());
        }
        if(model.getState()!=null&&model.getState()>0)
        {
            queryWrapper.eq("state",model.getState());
        }
        if(StringTool.isNotEmpty(model.getKeyWord()))
        {
            queryWrapper.and(wrapper -> wrapper.like("title", model.getKeyWord()).or().like("organizer", model.getKeyWord()));
        }
        queryWrapper.orderByDesc("crt_time");
        Page<JobFairSceneModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<JobFairSceneModel> jobFairSceneModelPage = jobFairSceneMapper.selectPage(page,queryWrapper);
        if (jobFairSceneModelPage==null) {
            return null;
        } else {
            return jobFairSceneModelPage;
        }
    }

    @Override
    public JobFairSceneVo bakGetJobFairList(JobFairSceneModel model) {
        Page<JobFairSceneModel> modelPage = getJobFairList(model);//查列表
        List<JobFairSceneModel> modelList = modelPage.getRecords();
        if (modelPage != null) {
            for (int i = 0; i < modelList.size(); i++) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Timestamp data = Timestamp.valueOf(modelList.get(i).getCrtTime());
                String currentSimpleDateFormat = simpleDateFormat.format(data);
                modelList.get(i).setCrtTime(currentSimpleDateFormat);
                String[] str = modelList.get(i).getSceneType().split(",");
                String sceneTypeText = "";
                for (int j = 0; j < str.length; j++) {
                    if(str[j].equals("1"))
                    {
                        sceneTypeText+="现场招聘,";
                    }
                    if(str[j].equals("2"))
                    {
                        sceneTypeText+="网络招聘,";
                    }
                    if(str[j].equals("3"))
                    {
                        sceneTypeText+="校园招聘,";
                    }
                }
                modelList.get(i).setSceneTypeText(sceneTypeText);
                modelList.get(i).setQyCount(jobFairCompanyService.countBySid(modelList.get(i).getId()));
            }
        }
        int count = (int) modelPage.getTotal();
        JobFairSceneVo vo = new JobFairSceneVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public JobFairSceneModel getJobFairById(int id) {
        return jobFairSceneMapper.selectById(id);
    }

    @Override
    public int getCountAll() {
        return jobFairSceneMapper.selectCount(null);
    }

    @Override
    public int insert(JobFairSceneModel model) {
        return jobFairSceneMapper.insert(model);
    }
    @Override
    public JobFairSceneVo jobFairRecommendation(Integer page1, Integer limit) {
        QueryWrapper<JobFairSceneModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("event_time_end", CommonHelp.getNowTimestamp());
        Page<JobFairSceneModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);
        Page<JobFairSceneModel> jobFairSceneModelPage = jobFairSceneMapper.selectPage(page,queryWrapper);
        JobFairSceneVo vo = new JobFairSceneVo();
        vo.setList(jobFairSceneModelPage.getRecords());
        vo.setCount((int) jobFairSceneModelPage.getTotal());
        return vo;
    }
}
